package com.b511.swpu.cementbackend.service.impl;

import com.b511.swpu.cementbackend.dto.output.NavigationOutDto;
import com.b511.swpu.cementbackend.dto.output.TreeOutputDto;
import com.b511.swpu.cementbackend.entity.SYS_Navigation;
import com.b511.swpu.cementbackend.entity.SysMenu;
import com.b511.swpu.cementbackend.repository.SYS_NavigationRepository;
import com.b511.swpu.cementbackend.repository.SysMenuRepository;
import com.b511.swpu.cementbackend.repository.Sys_Role_NavigationRepository;
import com.b511.swpu.cementbackend.service.SysRoleService;
import com.b511.swpu.cementbackend.dto.output.SysMenuOutputDto;
import com.b511.swpu.cementbackend.dto.output.SysRoleOutputDto;
import com.b511.swpu.cementbackend.repository.SysRoleRepository;
import com.b511.swpu.cementbackend.util.AnotherTreeUtil;
import com.b511.swpu.cementbackend.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    private final SysRoleRepository sysRoleRepository;

    private final SysMenuRepository sysMenuRepository;

    private final Sys_Role_NavigationRepository sys_role_navigationRepository;

    private final SYS_NavigationRepository sys_navigationRepository;

    @Autowired
    AnotherTreeUtil anotherTreeUtil;

    public SysRoleServiceImpl(SysRoleRepository sysRoleRepository, SysMenuRepository sysMenuRepository, Sys_Role_NavigationRepository sys_role_navigationRepository, SYS_NavigationRepository sys_navigationRepository) {
        this.sysRoleRepository = sysRoleRepository;
        this.sysMenuRepository = sysMenuRepository;
        this.sys_role_navigationRepository = sys_role_navigationRepository;
        this.sys_navigationRepository = sys_navigationRepository;
    }

    @Override
    public R getAllSysRole() {
        List<SysRoleOutputDto> result=new ArrayList<>();
        sysRoleRepository.findAll().forEach((role)->{
            SysRoleOutputDto sysRoleOutputDto=new SysRoleOutputDto();
            BeanUtils.copyProperties(role,sysRoleOutputDto);
            List<SysMenuOutputDto> list=new ArrayList<>();
            role.getSysMenus().forEach((menu)->{
                SysMenuOutputDto sysMenuOutputDto=new SysMenuOutputDto();
                BeanUtils.copyProperties(menu,sysMenuOutputDto);
                list.add(sysMenuOutputDto);
            });
            sysRoleOutputDto.setList(list);
            result.add(sysRoleOutputDto);
        });
        return R.ok().putData(result);
    }


    @Override
    public R getAllSysMenu() {
        List<SysMenu> sysMenus = sysMenuRepository.findAll();
        List<SysMenuOutputDto> result = new ArrayList<>();
        sysMenus.stream().forEach(sysMenu -> {
            SysMenuOutputDto dto = new SysMenuOutputDto();
            BeanUtils.copyProperties(sysMenu, dto);
            result.add(dto);
        });
        return R.ok().putData(result);
    }

    @Override
    public List<NavigationOutDto> getAllSysNavigationIDByRoleID(String roleID) {
        List<String> navigationIDs = sys_role_navigationRepository.findByRoleID(roleID);
        List<SYS_Navigation> navigations=new LinkedList<>();
        for(String id: navigationIDs){
            SYS_Navigation e = sys_navigationRepository.findByNavigationId(id);
            if (e!=null){
                navigations.add(e);
            }
        }
        return anotherTreeUtil.getNavigationTree(navigations);
    }

}
